﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yunwei.DAL;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;

namespace Yunwei.BizLogic
{
    public class ShoppingCartService : BaseService<ShoppingCart> , IShoppingCartService
    {
        public ShoppingCartService():base(RepositoryFactory.ShoppingCartRepository)
        {
        }

        public ShoppingCart FindByUserId(int user_Id, int food_Id)
        {
            return CurrentRepository.Find(s => s.User_Id == user_Id && s.Food_Id == food_Id);
        }

        public IQueryable<ShoppingCart> FindList(int number, int user_Id, int food_Id, int restaurant_Id, int orderCode)
        {
            return FindPageList(out number, 1, number, user_Id, food_Id, restaurant_Id, orderCode);
        }

        public IQueryable<ShoppingCart> FindPageList(out int totalRecord, int pageIndex, int pageSize, int user_Id, int food_Id, int restaurant_Id, int order)
        {
            IQueryable<ShoppingCart> sc = CurrentRepository.Entities;

            if (user_Id > 0) sc = sc.Where(s => s.Restaurant_Id == user_Id);
            if (food_Id > 0) sc = sc.Where(s => s.Restaurant_Id == food_Id);
            if (restaurant_Id > 0) sc = sc.Where(s => s.Restaurant_Id == restaurant_Id);

            sc = Order(sc, order);
            totalRecord = sc.Count();

            return PageList(sc, pageIndex, pageSize).AsQueryable();
        }

        public IQueryable<ShoppingCart> Order(IQueryable<ShoppingCart> entitys, int orderCode)
        {
            switch (orderCode)
            {
                //默认排序
                default:
                    entitys = entitys.OrderByDescending(cm => cm.Food_Id);
                    break;
            }
            return entitys;
        }

    }
}
